package com.yuerlive.rainbow.widget.behavior;


import android.animation.Animator;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.animation.Interpolator;

/**
 * CoordinatorLayout Behavior for a quick return footer
 * <p>
 * When a nested ScrollView is scrolled down, the quick return view will disappear.
 * When the ScrollView is scrolled back up, the quick return view will reappear.
 *
 * @author bherbst
 */
@SuppressWarnings("unused")
public class FooterBehavior extends CoordinatorLayout.Behavior<View> {
    private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();

    private int mDySinceDirectionChange;
    private boolean mIsShowing;
    private boolean mIsHiding;

    public FooterBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
        /*if (dy > 0 && mDySinceDirectionChange < 0
                || dy < 0 && mDySinceDirectionChange > 0) {
            // We detected a direction change- cancel existing animations and reset our cumulative delta Y
            mDySinceDirectionChange = 0;
        }*/

        mDySinceDirectionChange += dy;

        if (mDySinceDirectionChange > child.getHeight()) {
            mDySinceDirectionChange = child.getHeight();
        } else if (mDySinceDirectionChange < 0) {
            mDySinceDirectionChange = 0;
        }

        child.setTranslationY(mDySinceDirectionChange);
        if (mCallback != null) {
            mCallback.onCallback(mDySinceDirectionChange);
        }
    }


    public static interface Callback {
        void onCallback(int translationY);
    }

    public static void setCallback(Callback callback) {
        mCallback = callback;
    }

    private static Callback mCallback;


    public static void removeCallback() {
        mCallback = null;
    }

}
